<?php
namespace app\oem\controller;
use think\Controller;
use think\Db;
use app\common\controller\OemBaseController;

class Count extends OemBaseController
{
	//统计
     public function summary()
    {
    	$user_level=session("user_level");
		if($user_level==1){//oem
			$where['oem_id']=session('d_id');
		}elseif($user_level==2){//省代理
			$where['agency_id1']=session('d_id');
		}elseif($user_level==3){
			$where['agency_id2']=session('d_id');
		}	
		$mems=db('info_user')->where($where)->count();
		$agents=db('info_agent')->where($where)->count();
    	$where['state']=2;
		$list=db("log_paylog")->where($where)->field("uid,balance,order_type,score,sy_type,payment_type")->select();
		$data["mems"]=$mems;
		$data["agents"]=$agents;
		$data['mem_orders']=0;//会员消费订单笔数
		$data['mem_orders_ms']=0;//会员消费总额
		$data['sk_orders']=0;//散客消费订单笔数
		$data['sk_orders_ms']=0;//散客消费金额
		$data['sm_orders']=0;//扫码支付订单笔数
		$data['sm_orders_ms']=0;//扫码支付总额
		$data['glj_ms']=0;//鼓励金总额
		$data['glj_zs']=0;//鼓励金赠送
		$data['glj_dk']=0;//鼓励金抵扣
		$data['sc_orders']=0;//商城订单
		$data['sc_orders_ms']=0;//商城订单金额
		if($list){
			foreach($list as $k=>$v){
				if($v['payment_type']==1){
					$data['glj_zs']+=$v['score'];
				}elseif($v['payment_type']==2){
					$data['glj_dk']+=$v['score'];
				}
				$data['glj_ms']+=$v['score'];
				if($v['uid']){
					$data['mem_orders']++;
					$data['mem_orders_ms']+=$v['balance'];
				}else{
					$data['sk_orders']++;
					$data['sk_orders_ms']+=$v['balance'];
				}
				 if($v['order_type']==1){
				 	$data['sc_orders']++;
					$data['sc_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==2){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }elseif($v['order_type']==6&&$v['uid']){
				 	$data['sm_orders']++;
					$data['sm_orders_ms']+=$v['balance'];
				 }
			}
		}
		$mp=array('jz'=>$data['sk_orders_ms'],"sm"=>$data['sm_orders_ms'],'sc'=>$data['sc_orders_ms']);
		$this->assign('mp',json_encode($mp));
		$this->assign("data",$data);
    	return $this->fetch();
    }
   //明细查询
    public function commission()
    {
	   	if(request()->isPost()){
    		
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
    	}
		/*if(session('user_level')==1){
			$where['a.oem_id'] = session('d_id');
		}elseif(session('user_level')==2){
			$where['a.agency_id'] = session('d_id');
		}elseif(session('user_level')==3){
			$where['a.city_id'] = session('d_id');
		}*/
		
		$where['a.o_id'] = session('d_id');
		//$time="DATE_FORMAT(add_time,'%Y-%m-%d')";
    	$list=Db::table('log_oem_commission')->alias('a')
		    ->field('a.id,a.add_time,a.balance,a.order_type,a.message')
		    //->group($time."desc")
		    ->where($where)
		    ->paginate(20);
        $this->assign('list',$list);
		
		$sumbalance = db('log_oem_commission')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sumbalance',$sumbalance);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }
    
    public function detailed()
    {
    	if(request()->isPost()){
    		
    		$param=input('post.');
			if(!empty($param['begin_time']) && !empty($param['end_time'])){
				$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
			}
			if(!empty($param['storelist']))$where['a.chain_id'] = $param['storelist'];
			if(!empty($param['ywylist']))$where['a.salesman_id'] = $param['ywylist'];
			if(!empty($param['dplist']))$where['a.agent_id'] = $param['dplist'];
			if(!empty($param['pay_type']))$where['a.pay_type'] = $param['pay_type'];
			if(!empty($param['order_sn']))$where['a.order_sn'] = ['like','%'.$param['order_sn'].'%'];
			
    	}

		$where['a.oem_id'] = session('oem_id');
    	$paylog = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
    	->paginate(8);
		$this->assign('paylog',$paylog);
		
		
		//获取省代
		$info_oem = db('info_oem')->where('user_level',2)->where('status',1)->select();
		$this->assign('info_oem',$info_oem);
		
		//获取总消费额
		$sumbalance = db('log_paylog')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sumbalance',$sumbalance);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }

    public function sdlist()
    {
    	//获取市代
		$data = db('info_oem')->where('agency_id',input('id'))->where('user_level',3)->where('status',1)->select();
		return json(["data"=>$data]);
    } 

    public function ywylist()
    {
    	//获取业务员
		$data = db('info_salesman')->where('agency_id2',input('id'))->select();
		//获取店铺
		$datas = db('info_agent')->where('agency_id2',input('id'))->select();
		return json(["data"=>$data,"datas"=>$datas]);
    } 

    public function storelist()
    {
    	//获取店铺所属门店
		$data = db('agent_store')->where('agent_id',input('id'))->select();
		return json(["data"=>$data]);
    } 
    
    
    //明细导出
    public function detail_dc(){
		$param=input();
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}
		if(!empty($param['storelist']))$where['a.chain_id'] = $param['storelist'];
		if(!empty($param['ywylist']))$where['a.salesman_id'] = $param['ywylist'];
		if(!empty($param['dplist']))$where['a.tid'] = $param['dplist'];
		if(!empty($param['pay_type']))$where['a.pay_type'] = $param['pay_type'];
		if(!empty($param['order_sn']))$where['a.order_sn'] = ['like','%'.$param['order_sn'].'%'];
				
		$where['a.oem_id'] = session('oem_id');
    	$list = db('log_paylog_copy')
    	->alias('a')
		->join('agent_store w','a.chain_id = w.id','LEFT')
		->field('a.*,w.store_name')
    	->where($where)
    	->select();
		

		$str ="订单号,日期,门店,项目,支付方式,消费金额,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$store_name = iconv('utf-8','gbk',$row['store_name']); 
			if($row['sy_type']==1){
				$syname = '记账';
			}elseif($row['sy_type']==2) {
				$syname = '扫码';
			}else{
				$syname = '商品';
			}
			$syname = iconv('utf-8','gbk',$syname); 
			
			if($row['pay_type']==1){
				$payname = '余额';
			}elseif($row['pay_type']==2) {
				$payname = '微信';
			}elseif($row['pay_type']==3) {
				$payname = '支付宝';
			}else{
				$payname = '银联';
			}
			$pay_type = iconv('utf-8','gbk',$payname); 
			$balance = iconv('utf-8','gbk','	'.$row['balance']);
			$str .= $order_sn.",".$add_time.",".$store_name.",".$syname.",".$pay_type.",".$balance.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='明细列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}


	//佣金报表导出
    public function details_dc(){
		$param=input();
		if(!empty($param['begin_time']) && !empty($param['end_time'])){
			$where['a.add_time'] = ['between time',[$param['begin_time'],$param['end_time']]];
		}

		if(session('user_level')==2){
			$where['a.agency_id'] = session('d_id');
		}elseif(session('user_level')==3){
			$where['a.city_id'] = session('d_id');
		}	
		$time="DATE_FORMAT(add_time,'%Y-%m-%d')";
    	$list=Db::table('log_oem_commission')->alias('a')
		    ->field('a.id,a.add_time,sum(a.balance) as balance,a.message')
		    ->group($time."desc")
		    ->where($where)
		    ->select();
		

		$str ="佣金,信息,时间,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$balance = iconv('utf-8','gbk',$row['balance'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$message = iconv('utf-8','gbk',$row['message'].'	');
			$str .= $balance.",".$message.",".$add_time.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='佣金报表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}

	//市代理明细查询
 	public function citydetailed()
    {		
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('syylist'))$where['a.clerk_id'] = input('syylist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('sy_type'))$where['a.sy_type'] = input('sy_type');
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agency_id2'] = session('d_id');	
		$where['a.order_type'] = ['not in','3,4,5'];	
		$where['a.sy_type'] = ['in','2'];		
    	$paylog = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->join('info_agent b','a.agent_id = b.id','LEFT')
		->field('a.*,w.store_name,b.agent_name')
    	->where($where)
		->order('a.add_time desc')
    	->paginate(10,false,['query'=>request()->param()]);
		$this->assign('paylog',$paylog);
		
		//获取店铺
		$info_agent = db('info_agent')->where('agency_id2',session('d_id'))->where('status',1)->select();
		$this->assign('info_agent',$info_agent);
		
		//获取业务员
		$sale_list = db('info_salesman')->where('agency_id2',session('d_id'))->select();
		$this->assign('sale_list',$sale_list);
		
		//获取总消费额
		$sumbalance = db('log_paylog')->alias('a')->where($where)->sum('a.balance');
		$this->assign('sumbalance',$sumbalance);
		
		//获取总分佣
		$commission = db('log_paylog')->alias('a')->where($where)->sum('a.agency_id2_commission');
		$this->assign('commission',$commission);
		$this->assign('empty','<span class="empty">没有数据</span>');
    	return $this->fetch();
    }

	//根据业务员获取商家
    public function salemanlist()
    {
    	//获取店铺所属门店
		$data = db('info_agent')->where('salesman_id',input('id'))->select();
		return json(["data"=>$data]);
    } 
	
	//明细导出
    public function citydetail_dc(){
		if(input('begin_time') && input('end_time')){
			$where['a.add_time'] = ['between time',[input('begin_time'),input('end_time')]];
		}
		if(input('store_name'))$where['a.chain_id'] = ['like','%'.input('store_name').'%'];
		if(input('syylist'))$where['a.clerk_id'] = input('syylist');
		if(input('pay_type'))$where['a.pay_type'] = input('pay_type');
		if(input('sy_type'))$where['a.sy_type'] = input('sy_type');
		if(input('state')){
			$where['a.state'] = input('state');
		}else{
			$where['a.state'] = 2;
		}
		if(input('order_sn'))$where['a.order_sn'] = ['like','%'.input('order_sn').'%'];

    	$where['a.agency_id2'] = session('d_id');	
		$where['a.order_type'] = ['not in','3,4,5'];	
		$where['a.sy_type'] = ['in','2'];		
    	$list = db('log_paylog')
    	->alias('a')
		->join('agent_store w','a.agent_id = w.agent_id and a.chain_id = w.id','LEFT')
		->join('info_agent b','a.agent_id = b.id','LEFT')
		->field('a.*,w.store_name,b.agent_name')
    	->where($where)
		->order('a.add_time desc')
    	->select();


		$str ="订单号,日期,门店,项目,支付方式,消费金额,"."\n";
        $str = iconv('utf-8','gbk',$str);
        foreach($list as $k=>$row){
        	$order_sn = iconv('utf-8','gbk',$row['order_sn'].'	');
			$add_time = iconv('utf-8','gbk',$row['add_time'].'	');
			$store_name = iconv('utf-8','gbk',$row['store_name']); 
			if($row['sy_type']==1){
				$syname = '记账';
			}elseif($row['sy_type']==2) {
				$syname = '扫码';
			}else{
				$syname = '商品';
			}
			$syname = iconv('utf-8','gbk',$syname); 
			
			if($row['pay_type']==1){
				$payname = '余额';
			}elseif($row['pay_type']==2) {
				$payname = '微信';
			}elseif($row['pay_type']==3) {
				$payname = '支付宝';
			}else{
				$payname = '银联';
			}
			$pay_type = iconv('utf-8','gbk',$payname); 
			$balance = iconv('utf-8','gbk','	'.$row['balance']);
			$str .= $order_sn.",".$add_time.",".$store_name.",".$syname.",".$pay_type.",".$balance.","."\n"; //用引文逗号分开 
        } 

	  	$filename ='明细列表.csv';   //设置文件名  
    	
        header("Content-type:text/csv"); 
        header("Content-Disposition:attachment;filename=".$filename); 
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0'); 
        header('Expires:0'); 
        header('Pragma:public'); 
        echo $str; 
        exit;
	}
}
